package org.jsemantic.servicerules.examples.energy;

import java.math.BigDecimal;
import java.util.Map;
import java.util.WeakHashMap;

import org.jsemantic.core.context.external.ExternalContext;
import org.jsemantic.core.context.external.MapExternalContext;
import org.jsemantic.core.session.factory.SemanticSessionFactory;
import org.jsemantic.servicerules.drools.session.factory.SemanticSessionFactoryImpl;
import org.jsemantic.servicerules.examples.energy.services.InvoiceService;
import org.jsemantic.servicerules.examples.energy.services.PrinterService;

public class InvoiceServiceFactory {

	private static String ruleFile = "org/jsemantic/servicerules/examples/energy/tariff.drl";

	private static SemanticSessionFactory factory = getSemanticSessionFactory();

	private InvoiceServiceFactory() {

	}

	public static InvoiceService getInstance() {
		InvoiceService service = new InvoiceService();
		service.setSemanticSessionFactory(factory);
		return service;
	}

	private static ExternalContext getExternalContext() {

		PrinterService printerService = new PrinterService();
		Map ctx = new WeakHashMap();
		ctx.put("printerService", printerService);

		ExternalContext context = new MapExternalContext(ctx);

		return context;
	}

	private static SemanticSessionFactory getSemanticSessionFactory() {
		SemanticSessionFactory factory = new SemanticSessionFactoryImpl(
				ruleFile);
		factory.setSessionVariables(getSessionVariables());
		factory.setExternalContext(getExternalContext());
		return factory;
	}

	private static Map getSessionVariables() {
		Map variables = new WeakHashMap();
		variables.put("kwh", new BigDecimal(0.091939));
		variables.put("ppower", new BigDecimal(1.618345));
		variables.put("months", new Integer(2));
		return variables;
	}
}
